package com.kk.config;

import com.kk.util.result.Result;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author: kevin
 * @data: 2018-09-04 11:32
 * @desc: 全局异常处理器
 */
@ControllerAdvice
@Log4j2
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handleException(Exception e) throws Exception {
        if (e instanceof MyException) {
            MyException myException = (MyException) e;
            return Result.error(myException.getCode(), e.getMessage());
        }else{
            log.error("系统异常", e);
            throw e;
        }
    }
}
